package com.forehope.portal.aslpUtil;

import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.AppAPI;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class CMSUtil {
    public ResponseObject getCmsDataByModeule(String moduleid, String sid, int nowPage, int pageNum,String type) {
        ResponseObject responseObject = ResponseObject.newOkResponse("操作成功");
        try {

            // 调用App
            String sourceAppId = "com.actionsoft.apps.cms";
            // aslp服务地址
            String aslp = "aslp://com.actionsoft.apps.cms/queryCmsDataByModule";
            // 参数定义列表
            Map params = new HashMap<String, Object>();
            //是否删除 1未删除 2已删除,非必填
            //            params.put("deleteflag", deleteflag);
            //是否返回阅读状态 1不返回 2返回,非必填
            params.put("returnIsRead", 2);
            params.put("sid", sid);
            if (!"list".equals(type)){
                //开始记录数,非必填
                params.put("start", 0);
                //每次加载的条数,非必填
                params.put("limit", 4);
            }else{
                //开始记录数,非必填
                params.put("start", nowPage*pageNum);
                //每次加载的条数,非必填
                params.put("limit", pageNum);
            }
            //栏目id,必填
            params.put("moduleid", moduleid);
            //发布状态   1已发布2未发布,非必填
            params.put("isclose", 1);
            AppAPI appAPI = SDK.getAppAPI();
            //查询某个栏目下所有信息
            ResponseObject ro = appAPI.callASLP(appAPI.getAppContext(sourceAppId), aslp, params);
            if (ro.isOk()) {
                responseObject = ro;
            } else {
                responseObject = ResponseObject.newErrResponse("查询失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseObject = ResponseObject.newErrResponse("查询失败！");
        } finally {
            return responseObject;
        }
    }

    /**
     * 获取未读消息数量
     *
     * @return
     */
    public ResponseObject getNum(UserContext userContext) {
        ResponseObject responseObject = ResponseObject.newOkResponse("操作成功");
        try {
            // 调用App
            String sourceAppId = "com.actionsoft.apps.notification";
            // aslp服务地址
            String aslp = "aslp://com.actionsoft.apps.notification/getUnreadCount";
            // 参数定义列表
            Map params = new HashMap<String, Object>();
            //用户uid,必填
            params.put("uid", userContext.getUID());
            AppAPI appAPI = SDK.getAppAPI();
            //获取未读消息数量
            ResponseObject ro = appAPI.callASLP(appAPI.getAppContext(sourceAppId), aslp, params);
            if (ro.isOk()) {
                responseObject = ro;
            } else {
                responseObject = ResponseObject.newErrResponse("查询失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseObject = ResponseObject.newErrResponse("操作失败！");
        } finally {
            return responseObject;
        }
    }


    /**
     * 获取信息详情
     *
     * @return
     */
    public ResponseObject getMessageInfo(UserContext userContext, String messageId) {
        ResponseObject responseObject = ResponseObject.newOkResponse("操作成功");
        try {
            // 调用App
            String sourceAppId = "com.actionsoft.apps.cms";
            // aslp服务地址
            String aslp = "aslp://com.actionsoft.apps.cms/queryMessageInfo";
            // 参数定义列表
            Map params = new HashMap<String, Object>();
            //信息编号,必填
            params.put("messageId", messageId);
            AppAPI appAPI = SDK.getAppAPI();
            //通过信息id查询信息详情
            ResponseObject ro = appAPI.callASLP(appAPI.getAppContext(sourceAppId), aslp, params);
            if (ro.isOk()) {
                responseObject = ro;
            } else {
                responseObject = ResponseObject.newErrResponse("查询失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseObject = ResponseObject.newErrResponse("操作失败！");
        } finally {
            return responseObject;
        }
    }
    /**
     * 阅读数量+1
     */
    public String readNum(String sessionId,String modelId) {
        String url = SDK.getPortalAPI().getPortalUrl() + "/r/jd?sid=" +sessionId + "&cmd=com.actionsoft.apps.cms_message_detail&messageId=" + modelId;
        String result = "";
        try {
            HttpGet request = new HttpGet(url);
            HttpClient httpClient = new DefaultHttpClient();
            RequestConfig config = RequestConfig.custom()
                    .setConnectTimeout(5000)
                    .setSocketTimeout(5000)
                    .build();
            request.setConfig(config);

            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
